//
//  HomeViewCell.swift
//  DKE-Album
//
//  Created by 杨东宝 on 2020/6/14.
//  Copyright © 2020 杨东宝. All rights reserved.
//

import UIKit

class DeviceListCell: UITableViewCell {
    var deviceModel:DeviceModel?
    
    lazy var backView: UIView = {
        let back = UIView()
        back.backgroundColor = UIColor.white
        back.layer.cornerRadius = 20
        back.layer.masksToBounds =  true
        contentView.addSubview(back)
        return back
    }()
    
    //MARK:- 选择按钮
    lazy var selectBtn: UIButton = {
        
        let btn =  UIButton.init(type: .custom)
        btn.setTitle("selectBtn".localized, for: .normal)
        btn.setTitleColor(HexRGB(0xffffff), for: .normal)
        btn.titleLabel?.font = DIMENSION_FONT_OF_SIZE(12)
        btn.setBackgroundColor(HexRGB(0x3D4466), forState: .normal)
        btn.layer.cornerRadius = DIMENSION_VALUE(10)
        btn.layer.masksToBounds = true
        btn.addTarget(self, action: #selector(selectBtnClick(_ : )), for: .touchUpInside)
        backView.addSubview(btn)
        return btn
        
    }()
    
    //MARK:- 名称
    lazy var nameLabe: UILabel = {
        let label = UILabel()
        label.text = ""
        label.font = DIMENSION_FONT_OF_SIZE(14)
        label.textColor = HexRGB(0x3D4466)
        backView.addSubview(label)
        return label
    }()

    //MARK:- 名称
    lazy var rssiLabe: UILabel = {
        let label = UILabel()
        label.text = ""
        label.font = DIMENSION_FONT_OF_SIZE(14)
        label.textColor = HexRGB(0x3D4466)
        backView.addSubview(label)
        return label
    }()
    //MARK:- mac地址
    lazy var label1Detail: UILabel = {
        let label = UILabel()
        label.text = "mac:3456789506-3023774"
        label.font = DIMENSION_FONT_OF_SIZE(12)
        label.textColor = HexRGB(0x999999)
        backView.addSubview(label)
        return label
    }()
    
    lazy var logoImage: UIImageView = {
        let image = UIImageView.init(image: UIImage.init(named: "bluetooth"))
        backView.addSubview(image)
        return image
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .clear
        initSubView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initSubView() {  
        logoImage.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(DIMENSION_VALUE(5))
            make.width.height.equalTo(DIMENSION_VALUE(30))
            make.top.equalToSuperview().offset(DIMENSION_VALUE(10))
        }
        nameLabe.snp.makeConstraints { (make) in
            make.left.equalTo(logoImage.snp_right).offset(DIMENSION_VALUE(5))
            make.bottom.equalTo(backView.snp_centerY).offset(DIMENSION_VALUE(-3))
        }
        rssiLabe.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabe.snp_right).offset(DIMENSION_VALUE(5))
            make.bottom.equalTo(backView.snp_centerY).offset(DIMENSION_VALUE(-3))
        }

        selectBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(DIMENSION_VALUE(-5))
            make.centerY.equalTo(backView.snp_centerY)
            make.width.equalTo(DIMENSION_VALUE(60))
            make.height.equalTo(DIMENSION_VALUE(20))
        }
        label1Detail.snp.makeConstraints { (make) in
            make.left.equalTo(logoImage.snp_right).offset(DIMENSION_VALUE(5))
            make.right.equalTo(selectBtn.snp_left).offset(DIMENSION_VALUE(-5))
            make.top.equalTo(backView.snp_centerY).offset(DIMENSION_VALUE(3))
        }
        backView.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview().offset(DIMENSION_VALUE(5))
            make.right.equalToSuperview().offset(DIMENSION_VALUE(-5))
            make.bottom.equalTo(logoImage.snp_bottom).offset(DIMENSION_VALUE(10))
            make.bottom.equalToSuperview().offset(DIMENSION_VALUE(-5))
        }
    }
}
extension DeviceListCell{
    //MARK:-选择按钮
    @objc func selectBtnClick(_ btn :UIButton) {
        
    }
}
//MARK:-数据赋值
extension DeviceListCell{
    var model:DeviceModel{
        set{
            deviceModel = newValue
            self.setModel(model: deviceModel!)
        }
        get{
            return deviceModel!
        }
    }
    func setModel(model:DeviceModel) {
        self.nameLabe.text = model.name
        if model.rssi != 0 {
            self.rssiLabe.text = String(describing: "\(model.rssi)")
        }else{
            self.rssiLabe.text = ""
        }
        self.label1Detail.text = (model.ID ?? "")
        if model.isWifi ?? false {
            logoImage.image = UIImage(named: "redian")
        }else{
            logoImage.image = UIImage(named: "bluetooth")
        }
    }
}
